﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
   public  class SqlServerSchoolYearDao : ISchoolYearDao
    {
        public SqlServerSchoolYearDao()
        {
            SQLUtility.TableName = "SchoolYear";
        }

        //Trả về đối tượng báo ăn
        private SchoolYear MakeSchoolYear(DataRow row)
        {
            Guid _id = Guid.Parse(row["SchoolYearID"].ToString());
            string _schoolYearNumber = row["SchoolYearNumber"].ToString();
            string _name = row["SchoolYearName"].ToString();
            int _year = int.Parse(row["Year"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());
         return new SchoolYear(_id, _schoolYearNumber, _name, _year, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<SchoolYear> MakeSchoolYears(DataTable dt)  
        {
            IList<SchoolYear> list = new List<SchoolYear>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeSchoolYear(row));
            }
            return list;
        }
        private DbParameter[] Take(SchoolYear SchoolYear)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@SchoolYearID", SchoolYear.Id);
            parameters[1] = new SqlParameter("@SchoolYearNumber", SchoolYear.SchoolYearNumber);
            parameters[2] = new SqlParameter("@SchoolYearName", SchoolYear.Name);
            parameters[3] = new SqlParameter("@Year", SchoolYear.Year);
            parameters[4] = new SqlParameter("@SchoolID", SchoolYear.SchoolID);

            return parameters;
        }

        public IList<Business_Objects.SchoolYear> GetSchoolYear()
        {
            SQLUtility.TableName = "SchoolYear";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeSchoolYears(dt);
        }

        public Business_Objects.SchoolYear GetSchoolYearByID(string SchoolYearID)
        {
            SQLUtility.TableName = "SchoolYear";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolYearID", SchoolYearID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeSchoolYear(row);
            }
        }

        public void InsertSchoolYear(Business_Objects.SchoolYear SchoolYear)
        {
            SQLUtility.TableName = "SchoolYear";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(SchoolYear), true);
        }

        public int UpdateSchoolYear(Business_Objects.SchoolYear SchoolYear)
        {
            SQLUtility.TableName = "SchoolYear";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(SchoolYear)); }
            catch { return -1; }
        }

        public int DeleteSchoolYear(Business_Objects.SchoolYear SchoolYear)
        {
            SQLUtility.TableName = "SchoolYear";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(SchoolYear)); }
            catch { return -1; }
        }


        public List<SchoolYear> GetSchoolYearBySchoolId(int schoolId)
        {
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectSchoolYearsByAndSchoolID", parameters);
            return MakeSchoolYears(dt).ToList(); 
        }


        public SchoolYear CapNhatNienKhoaHeThong(string schoolYearId,int schoolId)
        {
            DbParameter[] parameters = new DbParameter[2];

            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            parameters[1] = new SqlParameter("@SchoolYearID", schoolYearId);
            DataRow row = CommandProvider.GetDataRow("usp_UpdateSchoolYearLaNienKhoaHeThong", parameters);
            if (row == null) { return null; }
            else
            {
                return MakeSchoolYear(row);
            }
        }
    }
}
